package com.atguigu.edu.contorller;

import com.atguigu.edu.bean.PaperExamSorceStats;
import com.atguigu.edu.bean.PaperExamStats;
import com.atguigu.edu.service.PaperExamSorceService;
import com.atguigu.edu.util.DateFormatUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class PaperExamSorceStatsController {

    @Autowired
    private PaperExamSorceService paperExamSorceService;

    @RequestMapping("/paperscore")
    public String edu(@RequestParam(value = "date", defaultValue = "0") Integer date) {
        if (date == 0){
            date = DateFormatUtil.now();
        }
        List<PaperExamSorceStats> paperExamSorceStats = paperExamSorceService.selectExamSorce(date);

        StringBuilder jsonScore = new StringBuilder("{\n" +
                "  \"status\": 0,\n" +
                "  \"data\": {");
        jsonScore.append("\"total\": " + paperExamSorceStats.size() + ",");
        jsonScore.append("   \"columns\": [\n" +
                "      {\n" +
                "        \"name\": \"试卷名称\",\n" +
                "        \"id\": \"paper_title\"\n" +
                "      },\n" +
                "      {\n" +
                "        \"name\": \"不及格人数60以下\",\n" +
                "        \"id\": \"less60\"\n" +
                "      },\n" +
                "      {\n" +
                "        \"name\": \"60-70及格\",\n" +
                "        \"id\": \"from60to70\"\n" +
                "      },\n" +
                "      {\n" +
                "        \"name\": \"70-90良好\",\n" +
                "        \"id\": \"from70to90\"\n" +
                "      },\n" +
                "      {\n" +
                "        \"name\": \"90以上 优秀\",\n" +
                "        \"id\": \"above90\"\n" +
                "      }\n" +
                "    ], \"superHeaders\": \n" +
                "      [\n" +
                "        {\n" +
                "          \"name\": \"粒度\",\n" +
                "          \"colspan\": 1\n" +
                "        },\n" +
                "        {\n" +
                "          \"name\": \"统计\",\n" +
                "          \"colspan\": 4\n" +
                "        }\n" +
                "      ]\n" +
                "    ,\"rows\": [");

        for (int i = 0; i < paperExamSorceStats.size(); i++) {
            PaperExamSorceStats examSorceStats = paperExamSorceStats.get(i);
            jsonScore.append("      {\n" +
                    "        \"paper_title\": \"" +  examSorceStats.getPaper_title() + "\",\n" +
                    "        \"less60\": \"" + examSorceStats.getLess60() + "\",\n" +
                    "        \"from60to70\": \"" + examSorceStats.getFrom60to70() + "\",\n" +
                    "        \"from70to90\": \"" + examSorceStats.getFrom70to90() + "\",\n" +
                    "        \"above90\": " + examSorceStats.getAbove90() + "\n" +
                    "      }");
            if (i < paperExamSorceStats.size() - 1) {
                jsonScore.append(",");
            }
        }
        jsonScore.append("]}}");

        String replace = null;
        if (jsonScore.toString().contains("\\")){
            replace = jsonScore.toString().replace("\\", "_");
            return replace;
        }else {
            return jsonScore.toString();
        }
    }
}
